package com.neo.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neo.dao.CheckItemDao;
import com.neo.entity.PageResult;
import com.neo.entity.QueryPageBean;
import com.neo.pojo.CheckItem;
import com.neo.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 检查项服务
 */
@Service(interfaceClass = CheckItemService.class, loadbalance = "roundrobin")
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        //PageHelper分页
        PageHelper.startPage(pageBean.getCurrentPage(), pageBean.getPageSize());

        Page<CheckItem> page = checkItemDao.selectByCondition(pageBean.getQueryString());
        PageInfo<CheckItem> pageInfo = new PageInfo<>(page);
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        return pageResult;
    }

    @Override
    public void delete(Integer id) {
        long count = checkItemDao.findCountByCheckItemId(id);
        if(count > 0){
            throw new RuntimeException("删除的检查项已关联检查组，无法删除");
        }
        checkItemDao.deleteById(id);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll(){
        return checkItemDao.findAll();
    }


}
